﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using AppToolkit.WPF.Services.UserMessageHandling;
using NUnit.Framework;
using UnitTestUtils.UI;

namespace AppToolkit.WPF.Tests.Services.UserMessageHandling.UI
{
    [TestFixture]
    public class MessageServiceTests
    {

        [Test]
        public void HistoryEmpty()
        {
            var target = new MessageService();
            Assert.IsTrue(target.MessageHistory.IsEmpty);
        }

        [Test]
        public void CurrentItemSetWhenMessageIsShown()
        {
            var target = new MessageService();

            var msg = new Message { Subject = "some subject" };
            target.ShowMessage(msg);

            Assert.IsTrue(target.ShowInfo);
            Assert.AreEqual(msg, target.MessageHistory.CurrentItem);

        }

        [Test]
        public void HideCommandSetsShowInfoToFalse()
        {
            var target = new MessageService();

            var msg = new Message { Subject = "some subject" };
            target.ShowMessage(msg);

            Assert.IsTrue(target.ShowInfo);

            target.HideInfoCommand.Execute(null);
            Assert.IsFalse(target.ShowInfo);
        }

        [Test, STAThread]
        public void ShowInfoResetToFalseAfter5secs()
        {
            var target = new MessageService();
            target.MessageShowTime = TimeSpan.FromMilliseconds(200);

            var msg = new Message { Subject = "some subject" };
            target.ShowMessage(msg);

            Assert.IsTrue(target.ShowInfo);
    
            //Dispatcher.CurrentDispatcher.WaitOne( () => !target.ShowInfo );
            Dispatcher.CurrentDispatcher.WaitOne(TimeSpan.FromMilliseconds(600));
            
            Assert.IsFalse(target.ShowInfo);
        }

        [Test, STAThread]
        public void ShowInfoIsNotResetIfIsFocusedIsSetToTrue()
        {
            var target = new MessageService();
            target.MessageShowTime = TimeSpan.FromMilliseconds(200);

            var msg = new Message { Subject = "some subject" };
            target.ShowMessage(msg);

            Assert.IsTrue(target.ShowInfo);
            
            target.IsFocused = true;

            Dispatcher.CurrentDispatcher.WaitOne(TimeSpan.FromMilliseconds(600));

            Assert.IsTrue(target.ShowInfo);
        }
    }
}
